package cn.javaee.springmvc.service;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MybatisSqlSessionFactory {
	private static SqlSessionFactory SqlSessionFactory;
	
	public static SqlSessionFactory getSqlSessionFactory() {
		if(SqlSessionFactory == null) {
			InputStream inputStream;
			try {
				inputStream  =Resources.getResourceAsStream("mybatis-config.xml");
				SqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			} catch (IOException e) {
				throw new RuntimeException(e.getCause());
			}
		}
		return SqlSessionFactory;
	}
	public static SqlSession opensession() {
		return getSqlSessionFactory().openSession();
	}
}
